package com.example.birdfriends.service.impl;

import com.example.birdfriends.dao.BirdDao;
import com.example.birdfriends.pojo.bird.Bird;
import com.example.birdfriends.pojo.bird.BirdFront;
import com.example.birdfriends.service.BirdService;
import com.example.birdfriends.utils.convert.ConvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BirdServiceImpl implements BirdService {
    @Autowired
    private BirdDao birdDao = null;
    @Autowired
    private ConvertService convertService = null;

    @Override
    public List<BirdFront> findBirdNamesAndImages(Long start, Long lineNum) {
        List<Bird> birds = birdDao.findBirdNamesAndImages(start, lineNum);
        return convertService.birds2BirdFronts(birds);
    }

    @Override
    public Long countBirdsNumber() {
        return birdDao.countBirdsNumber();
    }

    @Override
    public Long findMyBirdsNumber(Long authorId) {
        return birdDao.findMyBirdNumber(authorId);
    }

    @Override
    public List<String> findBirdSections(){
        return birdDao.findBirdSections();
    }

    @Override
    public Bird findBirdByBid(Long bid) {
        return birdDao.findBirdByBid(bid);
    }

    @Override
    public List<Bird> findBirdsByAuthorId(Long authorId, Long start, Long lineNum) {
        return birdDao.findBirdsByAuthorId(authorId, start, lineNum);
    }

    @Override
    public int addBirdEntry(Bird bird) {
        return birdDao.addBirdEntry(bird);
    }

    @Override
    public List<Bird> findBirdByNameAndSection(String birdname, String section){
        return birdDao.findBirdByNameAndSection(birdname, section);
    }

    @Override
    public int updateBirdImageByBid(Bird bird) {
        return birdDao.updateBirdImagesByBid(bird);
    }

    @Override
    public int deleteMyBirdEntry(Long bid) {
        return birdDao.deleteMyBirdEntry(bid);
    }

    @Override
    public int updateMyBirdEntry(Bird bird) {
        return birdDao.updateBirdEntryByBid(bird);
    }

    @Override
    public List<Bird> findBirdsBySearch(String section, String name, long start, long linNum) {
        int sectionLen = section.length(), nameLen =  name.length();
        if (sectionLen != 0 && nameLen != 0){  // 两个都不为空，按照两个共同搜索
            return birdDao.findBirdsBySectionAndBirdname(section, name, start, linNum);
        }
        if(sectionLen != 0){
            return birdDao.findBirdsBySection(section, start, linNum);
        }
        return birdDao.findBirdsByBirdName(name, start, linNum);
    }

    @Override
    public int countBirdsBySearch(String section, String name){
        int sectionLen = section.length(), nameLen =  name.length();
        if (sectionLen != 0 && nameLen != 0){  // 两个都不为空，按照两个共同搜索
            return birdDao.countBirdsBySectionAndBirdname(section, name);
        }
        if(sectionLen != 0){
            return birdDao.countBirdsBySection(section);
        }
        return birdDao.countBirdsByBirdName(name);
    }
}
